package com.web.portal.utils.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageUrlFileUtil {
    protected static Logger logger = LoggerFactory.getLogger(ImageUrlFileUtil.class);
    /**
     * @Title: getUrlFile
     * @Description:  从服务器获取图片流文件
     * @date 2018年6月26日下午2:15:32
     */
    public static InputStream getUrlFile(URL url) {
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
            httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
// 从服务器返回一个输入流
                inputStream = httpURLConnection.getInputStream();
            }
        } catch (MalformedURLException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return inputStream;
    }


}
